package com.wongleoi.weather_demo.api

import androidx.annotation.Keep
import com.wongleoi.weather_demo.model.Weather
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Query

@Keep
interface ApiServices {


    /**
     * 城市天气预报
     * @param key 应用key值
     * @param city 城市adcode值
     */
    @GET("weather/weatherInfo")
    suspend fun weatherInfo(
        @Query("key") key: String,
        @Query("city") city: String,
        @Query("extensions") extensions: String = "ALL",
        @Query("output") output: String = "JSON"
    ): NetListResponse<Weather>

    companion object {

        private val BASE_URL = "https://restapi.amap.com/v3/"
        val isDebug = true
        private val okHttpClient = OkHttpClient()
            .newBuilder()
            .addInterceptor(HttpLoggingInterceptor().apply {
                if (isDebug) level =
                    HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
            })

        val apiServices: ApiServices = Retrofit.Builder()
            .client(okHttpClient.build())
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(ApiServices::class.java)
    }
}

data class NetListResponse<T>(
    val status: String,
    val count: String,
    val info: String,
    val infocode: String,
    val forecasts: MutableList<T>?,
)